package com.vent.mirai.events;

import com.vent.common.core.redis.RedisCache;
import com.vent.common.utils.StringUtils;
import com.vent.mirai.constant.MiraiConstants;
import com.vent.share.service.ShareService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Friend;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.ListeningStatus;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.*;
import net.mamoe.mirai.message.data.Face;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import net.mamoe.mirai.message.data.PlainText;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Description: Mirai 事件监听
 *
 * @author ZiXiu
 * @date 2021/8/19 17:21
 */
@Slf4j
@Component
public class GroupEvent extends SimpleListenerHost {

    private ShareService shareService;

    public GroupEvent(ShareService shareService) {
        this.shareService = shareService;
    }

    /**
     * 收到好友消息
     */
    @EventHandler
    public ListeningStatus onPrivateMessage(FriendMessageEvent event) {
        if (MiraiConstants.MAKE_NEW.equals(event.getMessage().contentToString())) {
            Map<String, List<Map<String, String>>> stringListMap = shareService.sendShareMakeNewList();
            // 今日可申购新债
            MessageChainBuilder messagesBuilder = new MessageChainBuilder()
                    .append(new Face(Face.BIAN_PAO))
                    .append("今日可申购新债：\n\n");
            List<Map<String, String>> todayList = stringListMap.get("todayList");
            if (StringUtils.isEmpty(todayList)) {
                messagesBuilder.append(new Face(Face.FA_DOU)).append("今日暂无新债！");
            } else {
                todayList.forEach(todayMap -> {
                    messagesBuilder.append(new Face(Face.HONG_BAO)).append(todayMap.get("bond_name") + "\n");
                });
            }
            // 即将开始申购新债
            messagesBuilder.append("\n\n--------------\n\n").append(new Face(Face.HE_CAI)).append("即将开始申购的新债：\n\n");
            List<Map<String, String>> comingSoonList = stringListMap.get("comingSoonList");
            if (StringUtils.isEmpty(comingSoonList)) {
                messagesBuilder.append(new Face(Face.FA_DOU)).append("近期暂无新债！");
            } else {
                comingSoonList.forEach(comingSoonMap -> {
                    messagesBuilder.append(new Face(Face.HONG_BAO)).append(comingSoonMap.get("bond_name") + " " + comingSoonMap.get("sub_date") + "\n");
                });
            }
            event.getSubject().sendMessage(messagesBuilder.build());
        } else if (MiraiConstants.OPEN_REMIND.equals(event.getMessage().contentToString())) {
            event.getSubject().sendMessage(new Face(Face.JING_LI).plus(" 开启新债申购提醒！")
                    .plus("\n\n我将会在新债发行当天的早上9点提醒您!"));
            shareService.openRemind(event.getSubject().getId());
        } else if (MiraiConstants.CLOSE_REMIND.equals(event.getMessage().contentToString())) {
            event.getSubject().sendMessage(new Face(Face.emm).plus(" 关闭新债申购提醒！"));
            shareService.closeRemind(event.getSubject().getId());
        } else {
            event.getSubject().sendMessage(new Face(Face.WEN_HAO_LIAN).plus(" 我没有那么聪明，不懂您的意思呢。" +
                    "\n\n回复“开启”即可开启新债申购提醒功能！" +
                    "\n\n回复“关闭”即可关闭新债申购提醒功能！" +
                    "\n\n回复“打新”即可获取新债发行列表，提早知晓新债动态！"));
        }
        //保持监听
        return ListeningStatus.LISTENING;
    }

    /**
     * 一个账号请求添加机器人为好友
     */
    @EventHandler
    public ListeningStatus onPrivateMessage(NewFriendRequestEvent event) {
        // 自动同意
        event.accept();
        //保持监听
        return ListeningStatus.LISTENING;
    }

    /**
     * 成功添加了一个新好友
     */
    @EventHandler
    public ListeningStatus onFriendAddEventMessage(FriendAddEvent event) {
        event.getFriend().sendMessage(new PlainText("您好！我是打新提醒机器人！").plus(new Face(Face.WO_ZUI_MEI))
                .plus("\n\n我可以在新债发行当天提醒您！").plus(new Face(Face.doge))
                .plus("\n\n回复“开启”即可开启新债申购提醒功能！").plus(new Face(Face.JING_LI))
                .plus("\n\n回复“打新”即可获取新债发行列表，提早知晓新债动态！").plus(new Face(Face.HUAI_XIAO)));
        //保持监听
        return ListeningStatus.LISTENING;
    }

}
